﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using WF.Common;
using SourceCode.Workflow.Client;

namespace WF.Web.Pages.WorkSpace
{
    public partial class MyWorkitem : BasePage
    {
        public bool _vFlag = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            SetBlockRights();
            if (!IsPostBack || this.IsRebind)
            {
                if (_vFlag)
                    txtApprover.Text = base.CurrentUser.LoginName;
                this.LoadWorkList();
            }


        }

        private void LoadWorkList()
        {
            string curUserName = _vFlag ? txtApprover.Text.Trim() : base.CurrentUser.LoginName;
            Connection oK2Connection = WF.Common.WorkFlowHelper.GetK2Connection();
            WF.BusinessObject.SystemSetting sysSet = new BusinessObject.SystemSetting();
            string k2folder = sysSet.GetValueByParamName("K2WorkflowFolder");
            try
            {
                DataSet dsMe = null, dsShare = null, dsShareUser;

                //正常的待办事项
                dsMe = WorkFlowHelper.GetCurrentUserWorkListItem(oK2Connection, k2folder, curUserName);


                //委托的待办事项
                oK2Connection.RevertUser();
                dsShareUser = WorkFlowHelper.GetWorkListSharedADUser(null, curUserName);
                dsShare = WorkFlowHelper.GetCurrentUserWorkListItem(oK2Connection, k2folder, curUserName, dsShareUser);


                //所有分配的待办事项
                DataSet dsAll = new DataSet();
                dsAll.Merge(dsMe);
                dsAll.Merge(dsShare);

                DataView dv = dsAll.Tables[0].DefaultView;
                string filter = " 1=1 ";
                if (txtStartDate.Text.Trim() != "")
                {
                    filter += string.Format(" And StartDate >= '{0}'", DateTime.Parse(txtStartDate.Text.Trim()).ToString("yyyy-MM-dd"));
                }
                if (txtEndDate.Text.Trim() != "")
                {
                    filter += string.Format("  And  StartDate <= '{0}'", DateTime.Parse(txtEndDate.Text.Trim()).ToString("yyyy-MM-dd"));
                }
                if (this.lstApplyType.SelectedIndex > 0)
                {
                    filter += " And ProcessName = '" + this.lstApplyType.SelectedItem.Text.Trim() + "'";
                }

                //if (lstRegion.SelectedIndex > 0)
                //{
                //    filter += string.Format(@" And  RegionName ='{0}'", this.lstRegion.SelectedItem.Text.Trim());
                //}
                //if (lstDepartment.SelectedIndex > 0)
                //{
                //    filter += " And Folio Like '%" + this.lstDepartment.SelectedItem.Text.Trim() + "%'";
                //}

                if (txtPropertyName.Text.Trim() != "")
                {
                    filter += " And  Folio Like '%" + this.txtPropertyName.Text.Trim() + "%'";

                }
                if (rblType.SelectedValue != "0")
                {
                    if (rblType.SelectedValue == "1")
                    {
                        filter += " And Type <> '委托'";
                    }

                    if (rblType.SelectedValue == "2")
                    {
                        filter += " And Type = '委托'";
                    }
                }

                dv.RowFilter = filter;
                dv.Sort = "ProcessName,StartDate Desc,RegionName,Folio";

                //ds.Tables[0].DefaultView.Sort = "ProcessName,StartDate Desc";
                this.dataListView.DataSource = dv;
                this.dataListView.DataBind();

                this.lblCount.Text = "(" + dv.Count.ToString() + ")";


            }
            catch (Exception ex)
            {
                CentaUtil.SendExceptionMessage(this.Page, ex);
            }
            finally
            {
                oK2Connection.Close();
            }
        }

        private void LoadWorkList2()
        {
            Connection oK2Connection = WF.Common.WorkFlowHelper.GetK2Connection();
            WF.BusinessObject.SystemSetting sysSet = new BusinessObject.SystemSetting();
            string k2folder = sysSet.GetValueByParamName("K2WorkflowFolder");
            try
            {
                DataSet dsMe = null, dsShare = null, dsShareUser;

                SessionManager sm = new SessionManager();

                //正常的待办事项
                dsMe = WorkFlowHelper.GetCurrentUserWorkListItem(oK2Connection, k2folder, sm.CurrentUser.LoginName);


                //委托的待办事项
                oK2Connection.RevertUser();
                dsShareUser = WorkFlowHelper.GetWorkListSharedADUser(null, sm.CurrentUser.LoginName);
                dsShare = WorkFlowHelper.GetCurrentUserWorkListItem(oK2Connection, k2folder, sm.CurrentUser.LoginName, dsShareUser);


                //所有分配的待办事项
                DataSet dsAll = new DataSet();
                dsAll.Merge(dsMe);
                dsAll.Merge(dsShare);

                DataView dv = dsAll.Tables[0].DefaultView;
                string filter = " 1=1 ";
                if (txtStartDate.Text.Trim() != "")
                {
                    filter += string.Format(" And StartDate >= '{0}'", DateTime.Parse(CentaUtil.Date8To10(txtStartDate.Text.Trim())).ToString("yyyy-MM-dd"));
                }
                if (txtEndDate.Text.Trim() != "")
                {
                    filter += string.Format("  And  StartDate <= '{0}'", DateTime.Parse(CentaUtil.Date8To10(txtEndDate.Text.Trim())).ToString("yyyy-MM-dd"));
                }
                if (this.txtPropertyName.Text.Trim() != "")
                {
                    filter += " And Folio Like '%" + this.txtPropertyName.Text.Trim() + "%'";
                }

                //if (lstRegion.SelectedIndex > 0)
                //{
                //    filter += string.Format(@" And  RegionName ='{0}'", this.lstRegion.SelectedItem.Text.Trim());
                //}
                //if (lstDepartment.SelectedIndex > 0)
                //{
                //    filter += " And Folio Like '%" + this.lstDepartment.SelectedItem.Text.Trim() + "%'";
                //}

                if (lstApplyType.SelectedIndex > 0)
                {
                    filter += " And  Folio Like '%" + this.lstApplyType.SelectedItem.Text.Trim() + "%'";

                }
                if (rblType.SelectedValue != "0")
                {
                    if (rblType.SelectedValue == "1")
                    {
                        filter += " And Type <> '委托'";
                    }

                    if (rblType.SelectedValue == "2")
                    {
                        filter += " And Type = '委托'";
                    }
                }

                dv.RowFilter = filter;
                dv.Sort = "ProcessName,StartDate Desc,RegionName,Folio";

                //ds.Tables[0].DefaultView.Sort = "ProcessName,StartDate Desc";
                this.dataListView.DataSource = dv;
                this.dataListView.DataBind();

                this.lblCount.Text = "(" + dv.Count.ToString() + ")";

            }
            catch (Exception ex)
            {
                CentaUtil.SendExceptionMessage(this.Page, ex);
            }
            finally
            {
                oK2Connection.Close();
            }
        }

        private void LoadWorkList1()
        {
            Connection oK2Connection = WF.Common.WorkFlowHelper.GetK2Connection();
            try
            {
                SessionManager sm = new SessionManager();
                DataSet ds = WorkFlowHelper.GetCurrentUserWorkListItem(oK2Connection, sm.CurrentUser.LoginName);
                ds.Tables[0].DefaultView.Sort = "ProcessName,StartDate Desc";
                this.dataListView.DataSource = ds.Tables[0].DefaultView;
                this.dataListView.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                oK2Connection.Close();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.LoadWorkList();
        }

        protected void lstApplyType_DataBound(object sender, EventArgs e)
        {
            if (this.lstApplyType.Items.FindByText("") == null)
            {
                this.lstApplyType.Items.Insert(0, new ListItem("--All--", ""));
                this.lstApplyType.SelectedIndex = 0;
            }
        }

        private void SetBlockRights()
        {
            string[] str = new ConfigHelper().HrBlockLimite.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            var dept = from a in new ConfigHelper().HrBlockLimite.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
                       where base.CurrentUser.DeptPath == a
                       select a;
            if (dept.Count() > 0)
                _vFlag = true;
            else
                _vFlag = false;
        }
    }
}